$(document).ready( function() {
	var ue=UE.getEditor('ueditorDetails');

	//获取项目名
	var contextPath = document.location.pathname; 
	var index =contextPath.substr(1).indexOf("/"); 
	contextPath = contextPath.substr(0,index+1); 
	
	var totalCount = $("#totalCount").val();
	var pSize = $("#pageSize").val();
	var pageIndex = $("#pageIndex").val();
	var pageCount = $("#pageCount").val();
	
	var promtArry = new Array()// 定义一个数组，放提示信息
	var promtNum = 0;
	
	$("#giDetails").on('hidden.bs.modal', function() {
		$(this).removeData("bs.modal");
	});
	
	//根君id判断是添加还是修改
	$("#optBtn").delegate('#saveBtn','click', function() {
		$("#giftInfo").attr("method","post");
		var giId = $("#giId").val();
		if (giId =="" || giId ==undefined || giId ==null) {
			$("#giftInfo").attr("action","save.do");
		} else {
			$("#giftInfo").attr("action","update.do");
		}
		$("#giftInfo").submit();
	});
	
	//分页
	$('#pagination2').jqPaginator({
		totalCounts:totalCount,
		pageSize: pSize,
		visiblePages: pageCount,
		currentPage: 1,
		prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
		page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
		next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
		onPageChange : function(num, type) {
			$("#pageIndex").val(num);
		var queryUrl = "queryByPage.do";
		$.ajax({
			url : queryUrl,
			dataType : 'json',
			data : {
				pageIndex :num,
				pageSize : pSize
			},
			cache : false,
			success : function(data) {
				$("#giList tr:not(:first)").remove();
				var results = data.pageResult.resultList;
				for (i = 0; i < results.length; i++) {
					var lg = results[i];
					var rowContent = "<tr>"
						+ "<td><input name='giId' id='giId' class='checkbox' type='checkbox' value='"
						+ lg.id
						+ "'/></td>"
						+ "<td>"
						+ lg.giftCode
						+ "</td>"
						+ "<td>"
						+ lg.giftName
						+ "</td>"
						+ "<td><div style=' width:100px; height:30px;overflow:hidden;'>" 
						+ "<a id='"+i+"' href = 'javascript:void(0)' name='imga'>" 
						+ "<img  src='"+contextPath+"/HRTWEB/images/23/" 
						+lg.pic 
						+ "' border='0' width='100px' />"
						+ "</a>" +
								"</div>" 
						+ "<div id='light_"
						+ i
						+"' class='white_content'>  " 
						+ "<a id='"+i+"' href = 'javascript:void(0)' name='bigImg'> " 
						+ "<img src='"+contextPath+"/HRTWEB/images/23/" 
						+ lg.pic 
						+ "' /> </a> </div>  " 
						+ "<div id='fade_"
						+ i
						+"' class='black_overlay'></div>" 
						+ "</td>"
						
						+ "<td>"
						+ lg.promotion
						+ "</td>"
						+ "<td>"
						+ lg.discount
						+ "</td>"
						+ "<td>"
						if (lg.ispush == 0) {
							rowContent = rowContent
									+ "下架";
						} else {
							rowContent = rowContent
							+ "<font color='red'>上架</font>";
						}
						+ "</td>";
						
						rowContent = rowContent
						+ "<td>" 
						+ "<button name='viewRule' objId='"
						+ lg.id 
						+ "' class='btn btn-sm-block'>查看规格库</button>" 
						+ "</td>"
						
						+ "<td>" 
						+ "<button name='editBtn' id='editBtn' objId='"
						+ lg.id
						+ "' class='btn btn-sm-block'>修改</button>" 
						+ "</td>" 
						+ "</tr>";
					$("#giList").append(rowContent);
					}
				},
				error : function(html) {
					return;
				}
			});
		}
	});
	
	/**原图片*/
	$("#giList").delegate('[name="imga"]', 'click',function() {
		var rowId=$(this).attr("id");
		$("#light_"+rowId).css("display","block");
		$("#fade_"+rowId).css("display","block");
		
	});
	/**图片放大*/
	$("#giList").delegate('[name="bigImg"]', 'click',function() {
		var rowId=$(this).attr("id");
		$("#light_"+rowId).css("display","none");
		$("#fade_"+rowId).css("display","none");
	});

	/**
	 * 全选当前页记录
	 */
	$("#giList").delegate('#selectAll', 'click', function() {
		var isSelectAll = $(this).prop('checked');
		$("input[name='giId']").each(function() {
			$(this).prop("checked", isSelectAll);
		});
	});
	
	/**
	 * 查看规格库按钮事件
	 */
	$("#giList").delegate('[name="viewRule"]','click',function() {
		var giId = $(this).attr("objId");
	
		var editUrl = contextPath+"/manage/giftDetail/list.do";
		document.location.href = editUrl + "?id=" + giId;
	});
	
	/**
	 * 修改按钮事件
	 */
	$("#giList").delegate('#editBtn', 'click', function() {
		//初始化表单的验证提示信息
		var i = 0;
		$(".jqtransform").Validform().resetForm();
		$(".Validform_checktip").each(function() {
			if(promtArry.length >= promtNum && promtArry != null){
				$(this).text(promtArry[i]);
				$(this).removeAttr("class");  
				$(this).attr("class","Validform_checktip");
				$(this).parent().prev().children().removeAttr("class");
				$(this).parent().prev().children().attr("class","inputxt Validform_error");
			}
			i++;
		});
		var giId = $(this).attr("objId");
		var editUrl = "show.do?giId=" + giId;
		$.ajax({
			url : editUrl,
			dataType : 'json',
			data : {
				id : giId
			},
			cache : false,
			success : function(data) {
				loadGI(data);
				$(".pictrueLi div[col='closes']").each(function(index){ 
					var aa = $(this).find("img").width();
					var bb = $(this).parent().find("img[title='imgPic']").width();
					$(this).css("left",bb-aa);
				});
			},
			error : function(html) {
				alert("Load Data failly");
				refresh();
			}
		});
	});
	
	
	/**
	  删除按钮事件
	 */
	$("#tools").delegate('#deleteBtn','click',function() {
		delChk = $('input[name="giId"]:checked');
		if (delChk.length > 0) {
			if (confirm("Are you sure delete the record?")) {
				var ids = "";
				var delUrl = "delete.do";
	            var idArr=new Array();
	            for (var i = 0; i < delChk.length; i++) {
	            	idArr.push(delChk.get(i).value);
	            }
	            $.ajax({
					url : delUrl,
					type: "POST",
					data : {
						ids : idArr.toString()
					},
					cache : false,
					success : function(
							html) {
						alert("Delete successfully");
						refresh();
					},
					error : function(
							html) {
						alert("Delete failly");
						refresh();
					}
				});
			} else {
				return;
			}
		} else {
			alert("Please select the record to delete");
			return;
		}
	});
	
	/**
	  上架按钮事件
	 */
	$("#tools") .delegate('#shelveBtn','click',function() {
		sheChk = $('input[name="giId"]:checked');
		if (sheChk.length > 0) {
			if (confirm("你确定要将礼品上架?")) {
				var ids = "";
				var sheUrl = "shelve.do";
				var idArr=new Array();
				for (var i = 0; i < sheChk.length; i++) {
					idArr.push(sheChk.get(i).value);
				}
				$.ajax({
					url : sheUrl,
					type: "POST",
					data : {
						ids : idArr.toString()
					},
					cache : false,
					success : function(
							html) {
						alert("上架成功");
						refresh();
					},
					error : function(
							html) {
						alert("上架失败");
						refresh();
					}
				});
			} else {
				return;
			}
		} else {
			alert("请选择要上架的礼品");
			return;
		}
	});
	
	/**
	  下架按钮事件
	 */
	$("#tools") .delegate('#unShelveBtn','click',function() {
		unSheChk = $('input[name="giId"]:checked');
		if (unSheChk.length > 0) {
			if (confirm("你确定要将礼品下架?")) {
				var ids = "";
				var unSheUrl = "unShelve.do";
				var idArr=new Array();
				for (var i = 0; i < unSheChk.length; i++) {
					idArr.push(unSheChk.get(i).value);
				}
				$.ajax({
					url : unSheUrl,
					type: "POST",
					data : {
						ids : idArr.toString()
					},
					cache : false,
					success : function(
							html) {
						alert("下架成功");
						refresh();
					},
					error : function(
							html) {
						alert("下架失败");
						refresh();
					}
				});
			} else {
				return;
			}
		} else {
			alert("请选择要下架的礼品");
			return;
		}
	});
	
	
	/**
	 * 添加
	 */
	$("#tools").delegate('#addGiftInfo', 'click', function() {
		$.ajax({
			url : "add.do",
			dataType : 'json',
			data : {
			},
			cache : false,
			success : function(data) {
				$("#checks textarea").removeAttr("readonly");
				$("#giDetails input").each(function() {
					$(this).prop("value", null);
					$(this).prop("readonly", false);
				});

				$("#giDetails textarea").each(function() {
					$(this).prop("value", null);

				});
				$("#ueditorDetails").each(function() {
					ue.setContent("");
					
				});
				
				$("#giDetails select").each(function() {
					$(this).prop("value", 0);
					
				});
				
				$("#list li").remove();
				$(".pictrueLi img").remove();
				
				$("#imgNumber").val("0");
				$("#imgNumLen").val("0");
				$("#imgNumLater").val("0");
				
				//初始化表单的验证提示信息
				var i = 0;
				$(".jqtransform").Validform().resetForm();
				$(".Validform_checktip").each(function() {
					if(promtArry.length > 0 && promtArry != null){
						$(this).text(promtArry[i]);
						$(this).removeAttr("class");  
						$(this).attr("class","Validform_checktip");
						$(this).parent().prev().children().removeAttr("class");
						$(this).parent().prev().children().attr("class","inputxt Validform_error");
					}
					i++;
				});
				loadGI(data);
			},
			error : function(html) {
				alert(html);
				alert("Load Data failly");
				refresh();
			}
		});
		//展示模态窗
		$("#giDetails").modal('show');
		$("#ueditorDetails").modal('show');
	});
	
	/**获取开始的所有验证信息*/
	$(".Validform_checktip").each(function() {
		promtNum++;
		var prompt = $(this).text();
		promtArry.push(prompt);
	});
	
	/**
	 * 修改页面上的默认值
	 */
	function loadGI(giftInfo) {
		//清除上传的图片
		$("#list li").remove();
		//给表单赋值
		$("#giId").val(giftInfo.id);
		$("#giftCode").val(giftInfo.giftCode);
		$("#giftName").val(giftInfo.giftName);
		$("#promotion").val(giftInfo.promotion);
		$("#discount").val(giftInfo.discount);
		$("#pic").val(giftInfo.pic);
		$("#unit").val(giftInfo.unit);
		$("#enterPrice").val(giftInfo.enterPrice);
		$("#salePrice").val(giftInfo.salePrice);
		$("#groupPrice").val(giftInfo.groupPrice);
		$("#mailPrice").val(giftInfo.mailPrice);
		$("#totals").val(giftInfo.totals);
		$("#clickCount").val(giftInfo.clickCount);
		$("#collection").val(giftInfo.collection);
		$("#detail").val(giftInfo.detail);
		$("#description").val(giftInfo.description);
		$("#isGroup").val(giftInfo.isGroup);
		$("#isPackage").val(giftInfo.isPackage);
		$("#isPromote").val(giftInfo.isPromote);
		$("#isMail").val(giftInfo.isMail);
		$("#ispush").val(giftInfo.ispush);
		if(giftInfo.details_Str!=null){
			//富文本编辑器
			ue.setContent(giftInfo.details_Str);
		}
		if(giftInfo.picStrs != null && giftInfo.picStrs.length > 0){
			$("#imgNumber").val(giftInfo.picStrs.length);
			$("#imgNumLater").val(giftInfo.picStrs.length);
			$(".pictrueLi li").remove();
			for(f=0;f<giftInfo.picStrs.length;f++){
				var picImg = giftInfo.picStrs[f];
				var picImgContent = "<li>" 
				+ "<div style='position:relative;'> " 
				+ "<div col='closes' imgNum='1' style='position:absolute;'> " 
				+ "<img src='t01575e539bf71e5f51.png' width='15px' /></div> " 
				+ "<img title='imgPic' id='uploadImg_"+f+"' name='uploadImg' src='"+picImg+"' width='80px' height='80px' />" 
				+ "</div></li>";
			$(".pictrueLi").append(picImgContent);
			}
		}
		$("#giDetails").modal('show');
	}

	function vaildate(form) {
		return true;
	}
	/**
	 * 刷新页面
	 * */
	function refresh() {
		window.location.reload();
	}
	
	//请选择查询方式的下拉列表
	$("#selectWays").delegate(this, 'change', function() {
		if($(this).val()=="0"){
			$(".defaultSelectWays").css("display","block");
			$(".typeSelectWays").css("display","none");
		}else{
			$(".defaultSelectWays").css("display","none");
			$(".typeSelectWays").css("display","block");
		}
	});
	//默认查询
	$("#defaultSelectWays").delegate(this, 'click', function() {
		alert("aaaaa===>"+$("#giIdAndName").val());
	});
	
	//分类查询的下拉列表
	$("#typeGiSelect").delegate(this, 'change', function() {
		$("#typeGiInput").val($(this).val());
		var typeGiParUrl = "typeGiSelectParent.do";
		var typeGiParId = $("#typeGiInput").val();
		
		//获取input的三个值
		var rankGiInputNum = $("#rankGiInput").val();
		
		$.ajax({
			url : typeGiParUrl,
			type: "POST",
			data : {
				typeId :typeGiParId,//分类父类
			},
			cache : false,
			success : function(data) {
				$("#typeGiSelectSon option:not(:first)").remove();
				var typeSonList = data.typeSonList;
				if(typeSonList.length>0 && typeSonList != null){
					$("#typeGiSelectSon").css("display","block");
					for (i = 0; i < typeSonList.length; i++) {
						var typeSon = typeSonList[i];
						var rowContent ="<option value='" 
							+ typeSon.id
							+ "'>" 
							+ typeSon.typeName
							+ "</option>";
						$("#typeGiSelectSon").append(rowContent);
					}
				}else{
					$("#typeGiSelectSon").css("display","none");
				}
				
				/*//查询
				if(rankGiInputNum=="0"){
					giftInforPage();
				}
				if(rankGiInputNum=="1"){
					giftInforPage();
				}
				if(rankGiInputNum=="2"){
					giftInforPage();
				}
				*/
				giftInforRankPage(rankGiInputNum);
				
			},
			error : function(html) {
				alert("查询失败");
				return;
			}
		});
		
	});
	
	//分类查询的子类的下拉列表
	$("#typeGiSelectSon").delegate(this, 'change', function() {
		$("#typeGiInput").val($(this).val());
		
		//获取input的三个值
		var rankGiInputNum = $("#rankGiInput").val();
		//查询
		giftInforRankPage(rankGiInputNum);
		
		
	});
	
	//其他查询的下拉列表
	$("#otherGiSelect").delegate(this, 'change', function() {
		$("#otherGiInput").val($(this).val());
		
		//获取input的三个值
		var rankGiInputNum = $("#rankGiInput").val();
		//查询
		giftInforRankPage(rankGiInputNum);
		
	});
	
	//排序查询的下拉列表
	$("#rankGiSelect").delegate(this, 'change', function() {
		$("#rankGiInput").val($(this).val());
		
		//获取input的三个值
		var rankGiInputNum = $("#rankGiInput").val();
		giftInforRankPage(rankGiInputNum);
		
	});
	
	function giftInforRankPage(rankGiInputNum){
		//查询
		if(rankGiInputNum=="0"){
			giftInforPage();
		}
		if(rankGiInputNum=="1"){
			giftInforPage();
		}
		if(rankGiInputNum=="2"){
			giftInforPage();
		}
	}
	
	//分页{
	function giftInforPage(){
		//分页
		$('#pagination2').jqPaginator({
			totalCounts:totalCount,
			pageSize: pSize,
			visiblePages: pageCount,
			currentPage: 1,
			prev: '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
			page: '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
			next: '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
			onPageChange : function(num, type) {
				$("#pageIndex").val(num);
				//获取input的三个值
				var typeGiInput = $("#typeGiInput").val();
				var otherGiInput = $("#otherGiInput").val();
				var rankGiInput = $("#rankGiInput").val();
				alert("aa///"+typeGiInput+","+otherGiInput+","+rankGiInput);
				
				var queryUrl = "queryByPageDate.do";
				$.ajax({
					url : queryUrl,
					dataType : 'json',
					data : {
						pageIndex :num,
						pageSize : pSize,
						typeGiInput:typeGiInput,
						otherGiInput:otherGiInput,
						rankGiInput:rankGiInput
					},
					cache : false,
					success : function(data) {
						$("#giList tr:not(:first)").remove();
						var results = data.pageResult.resultList;
						for (i = 0; i < results.length; i++) {
							var lg = results[i];
							var rowContent = "<tr>"
								+ "<td><input name='giId' id='giId' class='checkbox' type='checkbox' value='"
								+ lg.id
								+ "'/></td>"
								+ "<td>"
								+ lg.giftCode
								+ "</td>"
								+ "<td>"
								+ lg.giftName
								+ "</td>"
								+ "<td><div style=' width:100px; height:30px;overflow:hidden;'>" 
								+ "<a id='"+i+"' href = 'javascript:void(0)' name='imga'>" 
								+ "<img  src='"+contextPath+"/HRTWEB/images/23/" 
								+lg.pic 
								+ "' border='0' width='100px' />"
								+ "</a>" +
										"</div>" 
								+ "<div id='light_"
								+ i
								+"' class='white_content'>  " 
								+ "<a id='"+i+"' href = 'javascript:void(0)' name='bigImg'> " 
								+ "<img src='"+contextPath+"/HRTWEB/images/23/" 
								+ lg.pic 
								+ "' /> </a> </div>  " 
								+ "<div id='fade_"
								+ i
								+"' class='black_overlay'></div>" 
								+ "</td>"
								
								+ "<td>"
								+ lg.promotion
								+ "</td>"
								+ "<td>"
								+ lg.discount
								+ "</td>"
								+ "<td>"
								if (lg.ispush == 0) {
									rowContent = rowContent
											+ "下架";
								} else {
									rowContent = rowContent
									+ "<font color='red'>上架</font>";
								}
								+ "</td>";
								
								rowContent = rowContent
								+ "<td>" 
								+ "<button name='viewRule' objId='"
								+ lg.id 
								+ "' class='btn btn-sm-block'>查看规格库</button>" 
								+ "</td>"
								
								+ "<td>" 
								+ "<button name='editBtn' id='editBtn' objId='"
								+ lg.id
								+ "' class='btn btn-sm-block'>修改</button>" 
								+ "</td>" 
								+ "</tr>";
							$("#giList").append(rowContent);
						}
					},
					error : function(html) {
						alert("sdfsdhj");
						return;
					}
				});
			}
		});
	}
	
});





